#include "../thread_pool.h"
#include "../user.h"
#include "mysql.h"
/*--------------------------delimiter------------------*/
void add_file(VF_entry *entry,user_info *user){
    connect_mysql(user);
    //执行prepare操作，将stmt与模板化的sql语句绑定
    const char* sql = "INSERT INTO virtual_file_table (parent_id, filename, owner_id, md5, filesize, type, pwd) values(?,?,?,?,?,?,?)";
    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 7) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //设置参数
    int parentid = 0; //1
    char file_name[100];//2
    unsigned long filename_len = 0;
    int owner_id = 0;//3
    char md[512] = {0};//4
    unsigned long md_len = 0;
    int filesize = 0;//5
    char atype[3] = {0};
    unsigned long atype_len = 0;
    char pwd[100] = {0};//7
    unsigned long pwd_len = 0;

    //绑定参数
    MYSQL_BIND bind[7];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &parentid;
    bind[0].is_null = 0;
    bind[0].length = NULL;//指定字符串长度的缓冲区的首地址
                          //
    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = file_name;
    bind[1].is_null = 0;
    bind[1].length = &filename_len;//指定字符串长度的缓冲区的首地址

    bind[2].buffer_type = MYSQL_TYPE_LONG;
    bind[2].buffer = &owner_id;
    bind[2].is_null = 0;
    bind[2].length = NULL;//指定字符串长度的缓冲区的首地址
                          //
    bind[3].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[3].buffer = md;
    bind[3].is_null = 0;
    bind[3].length = &md_len;//指定字符串长度的缓冲区的首地址

    bind[4].buffer_type = MYSQL_TYPE_LONG;
    bind[4].buffer = &filesize;
    bind[4].is_null = 0;
    bind[4].length = NULL;//指定字符串长度的缓冲区的首地址
                          //
    bind[5].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[5].buffer = atype;
    bind[5].is_null = 0;
    bind[5].length = &atype_len;//指定字符串长度的缓冲区的首地址
                                //
    bind[6].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[6].buffer = pwd;
    bind[6].is_null = 0;
    bind[6].length = &pwd_len;//指定字符串长度的缓冲区的首地址
                              //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    /* strcpy(username, user->name); */
    /* name_len = strlen(username); */
    parentid = entry->parent_id;//参数1
    strcpy(file_name, entry->filename);//2
    puts(file_name);
    filename_len = strlen(file_name);
    owner_id = user->user_id;//3
    strcpy(md, entry->md5);//4
    md_len = strlen(md);
    filesize = entry->filesize;//5
    strcpy(atype,entry-> type);//6
    atype_len = strlen(atype);
    strcpy(pwd, entry->pwd);//7
    pwd_len = strlen(pwd);

    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    else {
        printf("QUERY OK, %ld row affected.\n",
               mysql_stmt_affected_rows(user->stmt));
    }
}
/*-------------------------delimiter------------------*/
void add_virtual_table(user_info* user, int parent_id, const char* filename, const char* md5, int filelen, const char* type) {
    connect_mysql(user);
    //执行prepare操作，将stmt与模板化的sql语句绑定
    const char* sql = "INSERT INTO virtual_file_table (parent_id, filename, owner_id, md5, filesize, type, pwd) values(?,?,?,?,?,?,?)";
    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 7) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //设置参数
    int parentid = 0; //1
    char file_name[200] = {0};//2
    unsigned long filename_len = 0;
    int owner_id = 0;//3
    char md[512] = { 0 };//4
    unsigned long md_len = 0;
    int filesize = 0;//5
    char atype[3] = { 0 };
    unsigned long atype_len = 0;
    char pwd[100] = { 0 };//7
    unsigned long pwd_len = 0;

    //绑定参数
    MYSQL_BIND bind[7];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &parentid;
    bind[0].is_null = 0;
    bind[0].length = NULL;//指定字符串长度的缓冲区的首地址
                          //
    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = file_name;
    bind[1].is_null = 0;
    bind[1].length = &filename_len;//指定字符串长度的缓冲区的首地址

    bind[2].buffer_type = MYSQL_TYPE_LONG;
    bind[2].buffer = &owner_id;
    bind[2].is_null = 0;
    bind[2].length = NULL;//指定字符串长度的缓冲区的首地址
                          //
    bind[3].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[3].buffer = md;
    bind[3].is_null = 0;
    bind[3].length = &md_len;//指定字符串长度的缓冲区的首地址

    bind[4].buffer_type = MYSQL_TYPE_LONG;
    bind[4].buffer = &filesize;
    bind[4].is_null = 0;
    bind[4].length = NULL;//指定字符串长度的缓冲区的首地址
                          //
    bind[5].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[5].buffer = atype;
    bind[5].is_null = 0;
    bind[5].length = &atype_len;//指定字符串长度的缓冲区的首地址
                                //
    bind[6].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[6].buffer = pwd;
    bind[6].is_null = 0;
    bind[6].length = &pwd_len;//指定字符串长度的缓冲区的首地址
                              //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    parentid = parent_id;//参数1
    strcpy(file_name, filename);//2
    filename_len = strlen(file_name);
    owner_id = user->user_id;//3
    strcpy(md, md5);//4
    md_len = strlen(md);
    filesize = filelen;//5
    strcpy(atype, type);//6
    atype_len = strlen(atype);
    strcpy(pwd, user->pwd);//7
    pwd_len = strlen(pwd);

    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    else {
        printf("QUERY OK, %ld row affected.\n",
               mysql_stmt_affected_rows(user->stmt));
    }
}

